import React, { Component } from 'react';
import {
    Form, Icon, Input, Button,
  } from 'antd';
// import {routerRedux} from 'dva'
// import {routerRedux } from 'dva/router'
import {connect} from 'react-redux';
import {getToken} from './../../redux/action'
import './Login.css'

class Login extends Component {
    constructor(props) {
        super(props);
        this.state ={

        }

    }

    componentWillMount() {
        console.log(1111)
        let token =localStorage.getItem('token')
        const {dispatch} = this.props;
        dispatch(getToken(token))
        if(token) {
            this.props.history.push('/admin')
        }
    }
    componentDidMount() {

    }
    handleSubmit = (e) => {
        e.preventDefault();
        let userInfo = this.props.form.getFieldsValue();
        console.log(this.props,'this.props')
        this.props.form.validateFields((err, values) => {
          if (!err) {
            // console.log('Received values of form: ', values);
            console.log(userInfo)
            // let token = this.state.userName + this.state.password
           
            // let RedirectUrl = this.props.location.state ? this.props.location.state.from.pathname : '/'
            // 修改redux中的token值
            // this.props.changeActive(token)

            let token = 'nigbjanbmscbajsbnjana'
            if (!token) return
            localStorage.setItem('token', token)
            const {dispatch} = this.props;
            dispatch(getToken(token))
            this.props.history.push('/admin')


            // 登陆成功之后的跳转
            // this.props.history.push(RedirectUrl)
            // this.props.dispatch({
            //     type: '/admin'
            // });
            // this.props.history.push('/admin')
            // this.props.dispatch(routerRedux.push({
            //     pathname: "/admin"
            // }));
    
            // this.props.dispatch({
            //     type: '/admin',
            //     payload: values,
            //   });   
          }
        });
      }
    render() {
        const { getFieldDecorator } = this.props.form;
        return (
            <div className="login_container">
                <Form onSubmit={this.handleSubmit} className="login-form">
                <Form.Item>
                    {getFieldDecorator('userName', {
                        initialValue: 'admin',
                        rules: [{ required: true, message: 'Please input your username!' }],
                    })(
                        <Input prefix={<Icon type="user" style={{ color: 'rgba(0,0,0,.25)' }} />} placeholder="Username" />
                    )}
                </Form.Item>
                <Form.Item>
                    {getFieldDecorator('password', {
                        initialValue: '123456',
                        rules: [{ required: true, message: 'Please input your Password!' }],
                    })(
                        <Input prefix={<Icon type="lock" style={{ color: 'rgba(0,0,0,.25)' }} />} type="password" placeholder="Password" />
                    )}
                </Form.Item>
                <Form.Item>
                 
                    <Button type="primary" htmlType="submit" className="login-form-button">
                        Log in
                    </Button>
                
                </Form.Item>
            </Form>
            </div>
        );
    }
}

export default connect()(Form.create()(Login));